package com.mijie.fentsi;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.logging.Logger;

import javax.sql.RowSet;

import net.sf.json.JSONArray;

import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.dic.Dictionary;

import com.mijie.homi.search.util.DbNdsController;
import com.mijie.homi.search.util.Dbcontroller;
import com.mijie.homi.search.util.Dbcontroller_local;
import com.mijie.homi.search.util.god;
import com.mijie.homi.search.util.mycfg;
import com.mijie.homi.search.util.timeTester;
import com.mijie.query.fulltxtControllor;


/**
 * intend renva,event keyword
 * 
 * @author Administrator
 * 
 */
public class fentsiController extends fentsiOr {

	public static void main(String[] args) {

		try {
			Thread.sleep(15000);
		} catch (Exception e) {

			e.printStackTrace();
		}
		timeTester t = new timeTester("cb1");
		// Dbcontroller dbc=new Dbcontroller();
		fentsiController c = new fentsiController();
		c.getWordlistFromdb();// 1556 ms not concurrent vesion

		t.printUseTime();

		// RowSet

		System.out.println("--f");

		if (1 == 11)
			while (true) {
				try {
					Thread.sleep(1500000);
				} catch (Exception e) {

					e.printStackTrace();
				}
			}

	}

	public renvaEventC renvaEventCon;

	/**
	 * cbf all word include renva event
	 */
	public static List<String> words_include_renvaEventKw;
//	boolean isIni = false;

	public Dbcontroller dbc20;
	static Logger logger = Logger.getLogger("full_cb20x");

	/**
	 * word ,syvtsi,cywejon. renva.event
	 */
	public void iniFentsiWordlibFromdb() {
//		if (isIni)
//			return;
		String s = "中文分词工具包";
		// import org.wltea.analyzer.cfg.DefaultConfig;
		Configuration cfg = new mycfg();
		cfg=DefaultConfig.getInstance();;
		// 加载词库
		cfg.setUseSmart(true); // 设置智能分词
	 	Dictionary.initial(cfg);
	//	Dictionary.initial();

		Dictionary dictionary = Dictionary.getSingleton();

		// cbc
		FutureTask futureTask = new FutureTask(new Callable() {// 使用Callable接口作为构造参数
					public Object call() {
						timeTester t = new timeTester(
								"futureTask getWordlistFromdb_sysc ");
						List<String> li = getWordlistFromdb_sysc();
						t.printUseTime();
						logger.info("futureTask getWordlistFromdb_sysc time"
								+ t.pasttime);
						return li;
					}
				});
		Thread Thread1 = new Thread(futureTask);
		Thread1.setName(" thread 4 load  FentsiWordlib cbc");
		Thread1.setPriority(Thread.MAX_PRIORITY);
		Thread1.start();
		// //cbc

		// cb20
		final fentsiCa22 fentsiCa22_a = new fentsiCa22();
		;
		FutureTask futureTask_iniStopWordLib = new FutureTask(new Callable() {// 使用Callable接口作为构造参数
					public Object call() {
						timeTester t = new timeTester(
								"futureTask_iniStopWordLib");

						fentsiCa22_a.iniStopWordLib(null);
						t.printUseTime();
						logger.info("futureTask_iniStopWordLib time"
								+ t.pasttime);
						return null;

					}
				});
		Thread Thread1_futureTask_iniStopWordLib = new Thread(
				futureTask_iniStopWordLib);
		Thread1_futureTask_iniStopWordLib
				.setName(" Thread1_futureTask_iniStopWordLib");
		Thread1_futureTask_iniStopWordLib.setPriority(Thread.MAX_PRIORITY);
		Thread1_futureTask_iniStopWordLib.start();
		// //cb20

		timeTester tt1 = new timeTester(
				" query all words from db renva.event ");
		timeTester tt_all = new timeTester(" query all words from db   ");
		renvaEventC rec = new renvaEventC();
		rec.dbc = this.dbc20;
		rec.iniEventKeywordlibFromdb();
		rec.iniTypeKeywordLibFromDB();
		List<String> renvaKw = rec.renvaKws;
		List<String> eventkw = rec.eventKws;
		WordsLibController.RenvaEventKws = new ArrayList<String>();
		WordsLibController.RenvaEventKws.addAll(renvaKw);
		WordsLibController.RenvaEventKws.addAll(eventkw);
	
		tt1.printUseTime();
		logger.info("query all words from db renva.event  time:"
				+ tt1.pasttime);

		// cb18 ini cywejon.
		timeTester tt20 = new timeTester(" query cywejon");
		fulltxtControllor fc = new fulltxtControllor();
		fc.dbc = this.dbc20;
		fc.ini_CywejonSet();
		tt20.printUseTime();
		logger.info("query cywejon time:" + tt20.pasttime);

		god.getThreadResult(futureTask_iniStopWordLib);// cb20
		fentsiCa22_a.hanbinStopWordN_RenvaEventKws(); // cb20
		List<String> ext = (List<String>) god.getThreadResult(futureTask);
		// tt1.printUseTime();
		tt_all.printUseTime();
		logger.info("query all words from db    time:" + tt_all.pasttime);
		// logger.info("query all words from db renva.event.cywejon.wordmainN syvtsis times "+tt1.pasttime);

		// cbf
		List<String> allword = new ArrayList<String>();
		allword.addAll(ext);
		allword.addAll(renvaKw);
		allword.addAll(eventkw);

		words_include_renvaEventKw = allword;
		// /cbf

		timeTester tt2 = new timeTester("dictionary.addWords ");
		
 		if(WordsLibController.lastWordsLib!=null)
 		{
 			dictionary.disableWords(WordsLibController.lastWordsLib); 
//			
 		}
		WordsLibController.lastWordsLib=allword;
		dictionary.addWords(allword); // 自动添加自定义词
		 
		tt2.printUseTime();
		logger.info("dictionary.addWords times " + tt2.pasttime);

		isLoadDic = true;

		this.dictionaryCad = dictionary;
		renvaEventCon = rec;
	//	isIni = true;

	}

	/**
	 * zafei ,.concurrent version .
	 */
	@SuppressWarnings("unchecked")
	public List<String> getWordlistFromdb() {
		final Dbcontroller dbc = new Dbcontroller(0);
		List<FutureTask> task_li = new ArrayList<FutureTask>();
		int num = 450000;
		int concurrenNum = 1;
		int per = num / concurrenNum;
		for (int i = 0; i < concurrenNum; i++) {
			final int start = (i) * per;
			final int end = (i + 1) * per;

			FutureTask futureTask = new FutureTask(new Callable() {// 使用Callable接口作为构造参数
						public Object call() {

							// conn
							String sql = "select SQL_NO_CACHE word from word_main force index(PRIMARY) where 		  id > @min and id<=@max and del=0 ";
							sql = sql
									.replaceAll("@min", String.valueOf(start));
							sql = sql.replaceAll("@max", String.valueOf(end));
							System.out.println(sql);

							// if(1==1)return new HashSet();

							// dbc.setAutoCommit(false);
							Set set = new HashSet();
							// set=dbc.getSet_concurrent(sql, "word");
							set = dbc.getSet_rowset(sql);

							// dbc.commit();

							return set;

						}
					});
			Thread Thread1 = new Thread(futureTask);
			Thread1.setName(" thread 4 load  FentsiWordlib:_" + (i));
			Thread1.setPriority(Thread.MAX_PRIORITY);
			Thread1.start();
			task_li.add(futureTask);

		}

		List<String> li = new ArrayList<String>();
		Set<String> set_r = new HashSet();
		for (FutureTask tsk : task_li) {

			// if(tsk.isDone())
			{
				Set set = (Set) god.getThreadResult(tsk);
				set_r.addAll(set);
			}

		}
		dbc.close();

		li.addAll(set_r);
		return li;

	}

	public List<String> getWordlistFromdb_sysc() {
		final Dbcontroller dbc = new DbNdsController();
		List<FutureTask> task_li = new ArrayList<FutureTask>();
		int num = 450000;
		int concurrenNum = 1;
		int per = num / concurrenNum;
		Set set = new HashSet();
		for (int i = 0; i < concurrenNum; i++) {
			final int start = (i) * per;
			final int end = (i + 1) * per;

			// conn
			String sql = "select SQL_NO_CACHE word from word_main force index(PRIMARY) where 		  id > @min and id<=@max and del=0 ";
			sql = "select SQL_NO_CACHE word from word_main force index(PRIMARY) where 		  del=0 ";

			sql = sql.replaceAll("@min", String.valueOf(start));
			sql = sql.replaceAll("@max", String.valueOf(end));
			System.out.println(sql);

			// if(1==1)return new HashSet();

			// dbc.setAutoCommit(false);

			set = dbc.getSet_concurrent(sql, "word");

			// dbc.commit();

		}

		List<String> li = new ArrayList<String>();

		dbc.close();

		li.addAll(set);
		return li;

	}

}
